const stringRandom = require("string-random");
const { order_dtls, sys_address, sys_parameter, good, good_type, business, user, op } = require("../../middleware/baseModel");
module.exports = {
  // 获取验证码
  getVerificationCode() {
    let str = stringRandom(4, { letters: "1234567890" });
    return str;
  },

  // 获取商品池Id
  async isDefaultPool(id) {
    let goodRow = await good.findOne({ where: { id } });

    if (goodRow) {
      let goodTypeRow = await good_type.findOne({ where: { id: goodRow.good_type_id } });
      if (goodTypeRow.name === "默认池") {
        return true;
      }
    }
    return false;
  },

  // 是否开启发送短信
  async getEnableSMS() {
    let sysParameterRow = await sys_parameter.findOne({ where: { key: "enableSMS" } });
    if (sysParameterRow) {
      if (sysParameterRow.value === "1") {
        return true;
      } else {
        return false;
      }
    }
    return false;
  },

  // 获取商家结款周期
  async getMerchantPaymentCycle() {
    let sysParameterRow = await sys_parameter.findOne({ where: { key: "getMerchantPaymentCycle" } });

    if (sysParameterRow) {
      return sysParameterRow.value;
    }
    return 15;
  },

  // 删除订单时间
  async getDelOrderMinute() {
    let sysParameterRow = await sys_parameter.findOne({ where: { key: "delOrderMinute" } });
    if (sysParameterRow) {
      return sysParameterRow.value;
    }
    return 60;
  },

  // 是否开启零钱支付
  async getIsOpenChangePayment() {
    let seedNumberRow = await sys_parameter.findOne({ where: { key: "isOpenChangePayment" } });

    if (seedNumberRow) {
      return seedNumberRow.value;
    }

    return 0;
  },

  // 获取种子抵扣比列
  async getSeedNumberRate() {
    let seedNumberRow = await sys_parameter.findOne({ where: { key: "seedNumberRate" } });

    if (seedNumberRow) {
      return seedNumberRow.value;
    }
    return 0;
  },

  // 获取邮费
  async getShipFee(full_address) {
    let address1Str = await sys_parameter.findOne({ where: { key: "noPostageAddres10" } });
    if (address1Str) {
      let address1Array = address1Str.value.split("|");
      let rows = address1Array.filter((address) => full_address.indexOf(address) > -1);

      if (rows.length > 0) {
        return 10;
      }
    }

    let address2Str = await sys_parameter.findOne({ where: { key: "noPostageAddres20" } });
    if (address2Str) {
      let address2Array = address2Str.value.split("|");
      let rows = address2Array.filter((address) => full_address.indexOf(address) > -1);
      if (rows.length > 0) {
        return 20;
      }
    }

    return 0;
  },

  // 获取支付环境
  async getWxEnvironment() {
    let wxEnvironmentRow = await sys_parameter.findOne({ where: { key: "wxEnvironment" } });

    if (wxEnvironmentRow && wxEnvironmentRow.value === "0") {
      return false;
    }
    return true;
  },

  // 获取推广返现比例
  async getUserInviteRate() {
    let wxEnvironmentRow = await sys_parameter.findOne({ where: { key: "userInviteRate" } });

    if (wxEnvironmentRow) {
      return wxEnvironmentRow.value;
    }
    return 0;
  },
};
